package model;
import java.sql.*;

import connection.DbConnection;
import bean.PollUser;

public class AssignUserImpl {
	String isTemplate;
	String listname;
	String masterUsername;
	String username;	
	Connection conn; 
	
	public AssignUserImpl(String isTemplate, String listname, String masterUsername, String username){
		conn = new DbConnection().getConn();
		System.out.println("impl created");
		this.isTemplate = isTemplate;
		this.listname = listname;
		this.masterUsername = masterUsername;
		this.username = username;	 
	}
	/**
	 * Assigns the user to the list: returns false if failed, else true
	 * */
	public boolean assign(){
		try{
			String sql = "SELECT * FROM User_ParticipantList WHERE "+
			"lname='"+this.listname+"' AND "+
			"username='"+this.username+"' AND "+
			"masterUsername='"+this.masterUsername+"'";
			System.out.println(sql);
			ResultSet rs = conn.prepareStatement(sql).executeQuery();
			if(rs.next()){
				return false;
			}
		}catch(SQLException e){
			System.out.println(e.toString());
		}
		
		try{
			String sql = "INSERT INTO User_ParticipantList(lname, masterusername, username) VALUES(";
			sql+="'"+listname+"' ,";
			sql+="'"+masterUsername+"' ,";
			sql+="'"+username+"' )";
			System.out.println(sql);
			conn.prepareStatement(sql).executeQuery();
		}catch(SQLException e){
			System.out.println(e.toString());
			return false;
		}
		return true;
	}
	
	public boolean remove(){
		try{
		String sql = "DELETE FROM User_ParticipantList WHERE  "+
			"lname='"+this.listname+"' AND "+
			"username='"+this.username+"' AND "+
			"masterUsername='"+this.masterUsername+"'";
		System.out.println(sql);
		conn.prepareStatement(sql).executeQuery();
		return true;
		}catch(SQLException e){
			System.out.println(e.toString());
			return false;
		}
	}
}
